<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--侧边部门数据-->
      <el-col :xs="9" :sm="6" :md="4" :lg="4" :xl="4">
        <div class="head-container">
          <el-input
            v-model="deptName"
            clearable
            size="small"
            :placeholder="$t('dept.deptSearchPlaceholder')"
            prefix-icon="el-icon-search"
            class="filter-item"
            @input="getDeptDatas"
          />
        </div>
        <el-tree
          :data="deptDatas"
          :props="defaultProps"
          :expand-on-click-node="false"
          default-expand-all
          @node-click="handleNodeClick"
        />
      </el-col>
      <!--用户数据-->
      <el-col :xs="15" :sm="18" :md="20" :lg="20" :xl="20">
        <!--工具栏-->
        <div class="head-container">
          <div v-if="crud.props.searchToggle">
            <!-- 搜索 -->
            <el-input
              v-model="query.blurry"
              clearable
              size="small"
              :placeholder="$t('user.userSearchPlaceholder')"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
            <el-date-picker
              v-model="query.createTime"
              :default-time="['00:00:00','23:59:59']"
              type="daterange"
              range-separator=":"
              size="small"
              class="date-item"
              value-format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="$t('common.startTime')"
              :end-placeholder="$t('common.endTime')"
            />
            <el-select
              v-model="query.enabled"
              clearable
              size="small"
              :placeholder="$t('user.enabled')"
              class="filter-item"
              style="width: 90px"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in enabledTypeOptions"
                :key="item.key"
                :label="item.display_name"
                :value="item.key"
              />
            </el-select>
            <rrOperation :crud="crud" />
          </div>
          <crudOperation show="" :permission="permission" />
        </div>
        <!--表单渲染-->
        <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="630px">
          <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="96px">
            <el-form-item :label="$t('user.username')" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item :label="$t('user.phone')" prop="phone">
              <el-input v-model.number="form.phone" />
            </el-form-item>
            <el-form-item :label="$t('user.nickName')" prop="nickName">
              <el-input v-model="form.nickName" />
            </el-form-item>
            <el-form-item :label="$t('user.email')" prop="email">
              <el-input v-model="form.email" />
            </el-form-item>
            <el-form-item :label="$t('user.dept')" prop="dept.id">
              <treeselect
                v-model="form.dept.id"
                :options="depts"
                style="width: 178px"
                :placeholder="$t('user.selectDept')"
                @select="selectFun"
              />
            </el-form-item>
            <el-form-item :label="$t('user.job')" prop="job.id">
              <el-select v-model="form.job.id" style="width: 178px" :placeholder="$t('user.selectJob')" :title="$t('user.selectJob')">
                <el-option
                  v-for="(item, index) in jobs"
                  :key="item.name + index"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('user.sex')">
              <el-radio-group v-model="form.sex" style="width: 178px">
                <el-radio label="男">{{ $t('sex.male') }}</el-radio>
                <el-radio label="女">{{ $t('sex.female') }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item :label="$t('user.enabled')">
              <el-radio-group v-model="form.enabled" :disabled="form.id === user.id">
                <el-radio
                  v-for="item in dict.user_status"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item style="margin-bottom: 0;" :label="$t('user.roles')" prop="roles">
              <el-select
                v-model="form.roles"
                style="width: 437px"
                multiple
                :placeholder="$t('user.selectRole')"
                @remove-tag="deleteTag"
                @change="changeRole"
              >
                <el-option
                  v-for="item in roles"
                  :key="item.name"
                  :disabled="level !== 1 && item.level <= level"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="text" @click="crud.cancelCU">{{ $t('crud.cancel') }}</el-button>
            <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">{{ $t('crud.confirm') }}</el-button>
          </div>
        </el-dialog>
        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler" @sort-change="crud.doTitleOrder">
          <el-table-column :selectable="checkboxT" type="selection" width="55" />
          <el-table-column v-if="columns.visible('username')" :show-overflow-tooltip="true" prop="username" :label="$t('user.username')" sortable="custom" />
          <el-table-column v-if="columns.visible('nickName')" :show-overflow-tooltip="true" prop="nickName" :label="$t('user.nickName')" sortable="custom" />
          <el-table-column v-if="columns.visible('sex')" prop="sex" :label="$t('user.sex')" sortable="custom" />
          <el-table-column v-if="columns.visible('phone')" :show-overflow-tooltip="true" prop="phone" width="100" :label="$t('user.phone')" sortable="custom" />
          <el-table-column v-if="columns.visible('email')" :show-overflow-tooltip="true" width="125" prop="email" :label="$t('user.email')" sortable="custom" />
          <el-table-column v-if="columns.visible('dept')" :show-overflow-tooltip="true" width="110" prop="dept" :label="$t('user.job')" sortable="custom">
            <template slot-scope="scope">
              <div>{{ scope.row.dept.name }} / {{ scope.row.job.name }}</div>
            </template>
          </el-table-column>
          <el-table-column v-if="columns.visible('enabled')" :label="$t('user.enabled')" align="center" prop="enabled" sortable="custom">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.enabled"
                :disabled="user.id === scope.row.id"
                active-color="#409EFF"
                inactive-color="#F56C6C"
                @change="changeEnabled(scope.row, scope.row.enabled)"
              />
            </template>
          </el-table-column>
          <el-table-column v-if="columns.visible('createTime')" :show-overflow-tooltip="true" prop="createTime" width="140" :label="$t('be.createTime')" sortable="custom">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-permission="['admin','user:edit','user:del']"
            :label="$t('be.operate')"
            width="125"
            align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <udOperation
                :data="scope.row"
                :permission="permission"
                :disabled-dle="scope.row.id === user.id"
              />
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import crudUser from '@/api/system/user'
import { isvalidPhone } from '@/utils/validate'
import { getDepts } from '@/api/system/dept'
import { getAll, getLevel } from '@/api/system/role'
import { getAllJob } from '@/api/system/job'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Treeselect from '@riophae/vue-treeselect'
import { mapGetters } from 'vuex'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import i18n from '../../../lang'

let userRoles = []
// crud交由presenter持有
const adSearchFields = new Map([['username', i18n.t('user.username')], ['nickName', i18n.t('user.nickName')], ['sex', i18n.t('user.sex')], ['phone', i18n.t('user.phone')], ['email', i18n.t('user.email')], ['enabled', i18n.t('user.enabled')], ['createTime', i18n.t('be.createTime')]]) // 需要高级搜索的字段
const defaultCrud = CRUD({ title: i18n.t('user.TITLE'), url: 'api/users/page', exportUrl: 'api/users/download', crudMethod: { ...crudUser }, adSearchFields: adSearchFields })
const defaultForm = { id: null, username: null, nickName: null, sex: '男', email: null, enabled: 'false', roles: [], job: { id: null }, dept: { id: null }, phone: null }
export default {
  name: 'User',
  components: { Treeselect, crudOperation, rrOperation, udOperation, pagination },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  // 数据字典
  dicts: ['user_status'],
  data() {
    // 自定义验证
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error(i18n.t('user.phoneEmptyChk')))
      } else if (!isvalidPhone(value)) {
        callback(new Error(i18n.t('user.phoneFormatChk')))
      } else {
        callback()
      }
    }
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      deptName: '', depts: [], deptDatas: [], jobs: [], level: 3, roles: [],
      defaultProps: { children: 'children', label: 'name' },
      permission: {
        add: ['admin', 'user:add'],
        edit: ['admin', 'user:edit'],
        del: ['admin', 'user:del']
      },
      enabledTypeOptions: [
        { key: 'true', display_name: '激活(Active)' },
        { key: 'false', display_name: '禁用(Disable)' }
      ],
      rules: {
        username: [
          { required: true, message: i18n.t('user.usernameEmptyChk'), trigger: 'blur' },
          { min: 2, max: 20, message: i18n.t('user.usernameLengthRangeChk'), trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: i18n.t('user.nickNameEmptyChk'), trigger: 'blur' },
          { min: 2, max: 20, message: i18n.t('user.nickNameLengthRangeChk'), trigger: 'blur' }
        ],
        email: [
          { required: true, message: i18n.t('user.emailEmptyChk'), trigger: 'blur' },
          { type: 'email', message: i18n.t('user.emailFormatChk'), trigger: 'blur' }
        ],
        phone: [
          { required: true, trigger: 'blur', validator: validPhone }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  created() {
    this.$nextTick(() => {
      this.getDeptDatas()
      this.crud.toQuery()
      this.crud.msg.add = i18n.t('user.userAddOKMsg')
    })
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  methods: {
    changeRole(value) {
      userRoles = []
      value.forEach(function(data, index) {
        const role = { id: data }
        userRoles.push(role)
      })
    },
    [CRUD.HOOK.afterAddError](crud) {
      this.afterErrorMethod(crud)
    },
    [CRUD.HOOK.afterEditError](crud) {
      this.afterErrorMethod(crud)
    },
    afterErrorMethod(crud) {
      // 恢复select
      const initRoles = []
      userRoles.forEach(function(role, index) {
        initRoles.push(role.id)
      })
      crud.form.roles = initRoles
    },
    deleteTag(value) {
      userRoles.forEach(function(data, index) {
        if (data.id === value) {
          userRoles.splice(index, value)
        }
      })
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      this.getDepts()
      this.getRoles()
      this.getRoleLevel()
      form.enabled = form.enabled.toString()
    },
    // 打开编辑弹窗前做的操作
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.getJobs(this.form.dept.id)
      userRoles = []
      const roles = []
      form.roles.forEach(function(role, index) {
        roles.push(role.id)
        // 初始化编辑时候的角色
        const rol = { id: role.id }
        userRoles.push(rol)
      })
      form.roles = roles
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      if (!crud.form.dept.id) {
        this.$message({
          message: i18n.t('user.deptEmptyChk'),
          type: 'warning'
        })
        return false
      } else if (!crud.form.job.id) {
        this.$message({
          message: i18n.t('user.jobEmptyChk'),
          type: 'warning'
        })
        return false
      } else if (this.roles.length === 0) {
        this.$message({
          message: i18n.t('user.rolesEmptyChk'),
          type: 'warning'
        })
        return false
      }
      crud.form.roles = userRoles
      return true
    },
    // 获取左侧部门数据
    getDeptDatas() {
      const sort = 'id,desc'
      const params = { sort: sort }
      if (this.deptName) { params['name'] = this.deptName }
      getDepts(params).then(res => {
        if (res.code === 0) {
          this.deptDatas = res.data.content
        } else {
          this.crud.notify(res.msg, CRUD.NOTIFICATION_TYPE.ERROR)
        }
      })
    },
    // 获取弹窗内部门数据
    getDepts() {
      getDepts({ enabled: true }).then(res => {
        if (res.code === 0) {
          this.depts = res.data.content
        } else {
          this.crud.notify(res.msg, CRUD.NOTIFICATION_TYPE.ERROR)
        }
      })
    },
    // 切换部门
    handleNodeClick(data) {
      if (data.pid === 0) {
        this.query.deptId = null
      } else {
        this.query.deptId = data.id
      }
      this.crud.toQuery()
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm(i18n.t('crud.thisOperate') + this.dict.label.user_status[val] + '" ' + data.username + i18n.t('crud.continueTxt'), i18n.t('crud.dialogTitleHint'), {
        confirmButtonText: i18n.t('crud.confirm'),
        cancelButtonText: i18n.t('crud.cancel'),
        type: 'warning'
      }).then(() => {
        crudUser.edit(data).then(res => {
          if (res.code === 0) {
            this.crud.notify(this.dict.label.user_status[val] + i18n.t('common.success'), CRUD.NOTIFICATION_TYPE.SUCCESS)
          } else {
            this.crud.notify(res.msg, CRUD.NOTIFICATION_TYPE.ERROR)
          }
        }).catch(() => {
          data.enabled = !data.enabled
        })
      }).catch(() => {
        data.enabled = !data.enabled
      })
    },
    // 获取弹窗内角色数据
    getRoles() {
      getAll().then(res => {
        if (res.code === 0) {
          this.roles = res.data
        } else {
          this.crud.notify(res.msg, CRUD.NOTIFICATION_TYPE.ERROR)
        }
      }).catch(() => { })
    },
    // 获取弹窗内岗位数据
    getJobs(id) {
      getAllJob(id).then(res => {
        if (res.code === 0) {
          this.jobs = res.data.content
        } else {
          this.crud.notify(res.msg, CRUD.NOTIFICATION_TYPE.ERROR)
        }
      }).catch(() => { })
    },
    // 点击部门搜索对应的岗位
    selectFun(node, instanceId) {
      this.getJobs(node.id)
      this.form.job.id = null
    },
    // 获取权限级别
    getRoleLevel() {
      getLevel().then(res => {
        if (res.code === 0) {
          this.level = res.data.level
        } else {
          this.crud.notify(res.msg, CRUD.NOTIFICATION_TYPE.ERROR)
        }
      }).catch(() => { })
    },
    checkboxT(row, rowIndex) {
      return row.id !== this.user.id
    }
  }
}
</script>

<style scoped>
</style>
